package com.singzi.leetcode.N800;

import java.util.*;

public class T0715 {
    public static void main(String[] args) {
        RangeModule rangeModule = new RangeModule();
        rangeModule.addRange(1,2);
        rangeModule.addRange(3,7);
        rangeModule.addRange(5,10);
        rangeModule.addRange(11,12);
        rangeModule.addRange(8,12);
        System.out.println(rangeModule.map);
    }
}
class RangeModule {

    TreeMap<Integer, Integer> map;
    List<Integer>keyList;
    public RangeModule() {
        map = new TreeMap<>();
    }

    public void addRange(int left, int right) {
        keyList = new ArrayList<>();

        for(int key:map.keySet()){
            if(key >= left && key <= right) keyList.add(key);
        }
        if(!keyList.isEmpty())
        for(int key:keyList){
            map.remove(key);
        }
        if(map.floorKey(left) == null){
            map.put(left, 0);
            if(map.ceilingKey(right) == null || map.get(map.ceilingKey(right)) == 0){
                map.put(right,1);
            }

        }else{
            if(map.ceilingKey(right) == null){
                map.put(right,1);
            }
            if(map.get(map.floorKey(left)) == 0){
                if(map.ceilingKey(right) != null && map.get(map.ceilingKey(right)) == 0){
                    map.put(right, 1);
                }
            }if(map.get(map.floorKey(left)) == 1){
                if(map.ceilingKey(right) == null || map.get(map.ceilingKey(right)) == 0){
                    map.put(left, 0);
                    map.put(right, 1);
                }else if(map.get(map.ceilingKey(right)) == 1){
                    map.put(left, 0);
                }
            }
        }


    }

    public boolean queryRange(int left, int right) {

        if(map.floorKey(left) == null) return false;
        if(map.ceilingKey(right) == null) return false;
        return map.ceilingKey(map.floorKey(left) + 1) >= right;

    }

    public void removeRange(int left, int right) {
        keyList = new ArrayList<>();

        for(int key:map.keySet()){
            if(key >= left && key <= right) keyList.add(key);
        }
        if(!keyList.isEmpty())
            for(int key:keyList){
                map.remove(key);
            }
        if(map.floorKey(left) == null){
            if(map.ceilingKey(right) == null) {
                map.clear();
            }else if(map.get(map.ceilingKey(right)) == 1){
                map.put(right, 0);
            }

        }else{
            if(map.ceilingKey(right) == null){
                map.put(left,0);
            }
            if(map.get(map.floorKey(left)) == 0){
                if(map.ceilingKey(right) != null && map.get(map.ceilingKey(right)) == 0){
                    map.put(right, 1);
                }
            }if(map.get(map.floorKey(left)) == 1){
                if(map.ceilingKey(right) == null || map.get(map.ceilingKey(right)) == 0){
                    map.put(left, 0);
                    map.put(right, 1);
                }else if(map.get(map.ceilingKey(right)) == 1){
                    map.put(left, 0);
                }
            }
        }


    }
}